// pages/recPosition/recPosition.js
const app = getApp()
var url = require('../../utils/url.js');
var resId;
var page = 1;
var last_page = 0;
var type;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'rec_position/goods',
      data: { id: resId, page: page },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        that.setData({
          shops: that.data.shops.concat(res.data.data.data)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner:"",
    shops:"",
    indicatorDots: true,  //小点
    indicatorColor: "white",
    autoplay: true,  //是否自动轮播
    interval: 3000,  //间隔时间
    duration: 500,  //滑动时间
    indicatorActiveColor: "#DD2626",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    resId = options.id;
    that.getPosition()
    that.getGoods() 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getPosition() {
    var that = this;
    if (page > 1) {
      page--;
    }

    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'rec_position/ads',
      data: { id: resId},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        var msg = res.data.msg;
        if (res.data.code == 200) {
            that.setData({
              banner:res.data.data
            })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getGoods() {
    var that = this;
    if (page > 1) {
      page--;
    }

    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'rec_position/goods',
      data: { id: resId ,page:page},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        var msg = res.data.msg;
        if (res.data.code == 200) {
          last_page=res.data.data.last_page
          that.setData({
             shops:res.data.data.data
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    page=1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})